package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoServer userInfoServer;
    /**
     * 查询用户资料
     * 1.请求体：token
     * 2.请求参数：userID
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token , Long userID){

        /*//2.获取用户设置id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        //3.判断用户id
        if(userID == null){
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoServer.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity UpdateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
        /*//2.获取用户id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        //3.设置用户id
        userInfo.setId(UserHolder.getUserId());
        userInfoServer.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
